package org.example.ref;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class ApacheFTP {
    public static void main(String[] args) {
        String ip = "localhost";
        int port = 21;
        String name = "fyx";
        String pwd = "123456";
        //验证登录
        try {
            FTPClient ftp = new FTPClient();
            ftp.connect(ip, port);
            System.out.println(ftp.login(name, pwd));
            ftp.setFileType(FTP.BINARY_FILE_TYPE);
            ftp.setCharset(StandardCharsets.UTF_8);
            ftp.setControlEncoding("UTF-8");


            // 文件列表
            FTPFile[] files = ftp.listFiles();
            for (FTPFile file : files) {
                System.out.println(file.getName());
            }

            // 文件下载
            String fileName = "mf.jpeg";
            ftp.retrieveFile(fileName, new FileOutputStream(fileName));

            // 文件上传




        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}
